// WWL.include ( 'popbox' );

WWL.popmenu = function ( name )
{
	var menu = new WWL ( 'popmenu', name );

	menu.box = new WWL.popbox ( 'menu_' + name );
	menu.tbar = new WWL.toolbar ( 'tbar_' + name );

	menu._event_names = [];	// Toolbar has no "direct" events

	menu.tbar.mode = "V";

	// menu.box.set_size ( 200, 200 );

	// attrs:
	// txt
	// cback
	// style
	// ref_name
	// block_auto_close
	menu.add_item = function ( attrs )
	{
		var _obj = this;
		var old_cback = attrs [ 'cback' ];

		attrs [ 'cback' ] = function ( widget, name, div, ref_name )
			{
				if ( old_cback ) old_cback ( widget, name, div ); 
				if ( ! attrs [ 'block_auto_close' ] ) _obj.box.hide (); 
			};

		this.tbar.add_item ( attrs );
	};

	menu.clear = function () { this.tbar.clear (); };

	menu.set_parent = function ( div, direction )
	{
		this.box.set_parent ( div, direction );
	};

	menu.show = function ()
	{
		var s = this.tbar.to_string ();
		var e;

		e = this.box.show ();
		e.innerHTML = s;

		var w = e.firstChild.firstChild.offsetWidth;
		if ( w > this.box._width )
			e.style.width = w + "px";
		else
			e.firstChild.firstChild.style.width = this.box._width + "px";
	};

	menu.hide = function ()
	{
		this.box.hide ();
	};

	menu.show_hide = function ()
	{
		if ( this.box.is_hidden () ) return this.show ();

		this.box.hide ();
		
		return true;
	};

	menu.get_item = function ( name ) { return menu.tbar.get_item ( name ); };
	menu.disable_item = function ( name, disabled ) { return menu.tbar.disable_item ( name, disabled ); };

	return menu;
};

WWL.popmenu.get_instance = function ( instance_name )
{
	return WWL.get_instance ( "popmenu", instance_name );
};
